<?php

namespace app\shop\controller\Business;

use think\Controller;

class Address extends Controller
{
    // 当前对象模型
    protected $AddressModel = null;

    protected $BusinessModel = null;

    public function __construct()
    {
        parent::__construct();

        $this->AddressModel = model('Business.Address');
        $this->BusinessModel = model('Business.Business');
    }

    public function index()
    {
        $busid = $this->request->param('busid', 0, 'trim');

        $AddressData = $this->AddressModel->where(['busid' => $busid])->select();

        if (!$AddressData) {
            $this->error('暂无收货地址，请添加');
        }

        $this->success('查询成功', null, $AddressData);
    }

    public function add()
    {
        $params = $this->request->param();

        $Business = $this->BusinessModel->find($params['busid']);

        if (!$Business) {
            $this->error('用户不存在');
        }

        // 组装数据
        $data = [
            'busid' => $params['busid'],
            'address' => $params['address'],
            'mobile' => $params['mobile'],
            'consignee' => $params['consignee']
        ];

        // 默认地址
        if ($params['status'] == 1) {
            $status = $this->AddressModel->where(['busid' => $params['busid']])->update(['status' => 0]);

            if ($status === FALSE) {
                $this->error('设置默认地址失败');
            }

            $data['status'] = 1;
        }

        // 地区
        $parentpath = model('Region')->where(['code' => $params['code']])->value('parentpath');

        if (!$parentpath) {
            $this->error('选择地区不存在');
        }

        [$province, $city, $district] = explode(',', $parentpath);

        $data['province'] = $province;

        $data['city'] = $city;

        $data['district'] = $district;

        $result = $this->AddressModel->validate('common/Business/Address')->save($data);

        if ($result === FALSE) {
            $this->error($this->AddressModel->getError());
        } else {
            $this->success('新增收货地址成功');
        }
    }

    public function info()
    {
        $addrid = $this->request->param('addrid');
        $busid = $this->request->param('busid');

        $where = [
            'id' => $addrid,
            'busid' => $busid
        ];

        $AddressInfo = $this->AddressModel->where($where)->find();

        if (!$AddressInfo) {
            $this->error('该收货地址不存在');
        }

        $this->success('查询成功', null, $AddressInfo);
    }

    public function edit()
    {
        $params = $this->request->param();

        $where = [
            'id' => $params['addrid'],
            'busid' => $params['busid']
        ];

        $AddressInfo = $this->AddressModel->where($where)->find();

        if (!$AddressInfo) {
            $this->error('该收货地址不存在');
        }

        $data = [
            'id' => $params['addrid'],
            'busid' => $params['busid'],
            'address' => $params['address'],
            'mobile' => $params['mobile'],
            'consignee' => $params['consignee']
        ];

        // 默认地址
        if ($params['status'] == 1) {
            $status = $this->AddressModel->where(['busid' => $params['busid']])->update(['status' => 0]);

            if ($status === FALSE) {
                $this->error('设置默认地址失败');
            }

            $data['status'] = 1;
        } else {
            $data['status'] = 0;
        }

        // 地区
        $parentpath = model('Region')->where(['code' => $params['code']])->value('parentpath');

        if (!$parentpath) {
            $this->error('选择地区不存在');
        }

        [$province, $city, $district] = explode(',', $parentpath);

        $data['province'] = $province;

        $data['city'] = $city;

        $data['district'] = $district;

        $result = $this->AddressModel->isUpdate(true)->save($data);

        if ($result === FALSE) {
            $this->error('修改收货地址失败');
        } else {
            $this->success('修改收货地址成功');
        }
    }

    public function del()
    {
        $params = $this->request->param();

        $where = [
            'id' => $params['addrid'],
            'busid' => $params['busid']
        ];

        $AddressInfo = $this->AddressModel->where($where)->find();

        if (!$AddressInfo) {
            $this->error('该收货地址不存在');
        }

        $result = $this->AddressModel->destroy($params['addrid']);

        if ($result === FALSE) {
            $this->error('删除收货地址失败');
        } else {
            $this->success('删除收货地址成功');
        }
    }

    public function select()
    {
        $params = $this->request->param();

        $where = [
            'id' => $params['addrid'],
            'busid' => $params['busid']
        ];

        $AddressInfo = $this->AddressModel->where($where)->find();

        if (!$AddressInfo) {
            $this->error('该收货地址不存在');
        }

        $status = $this->AddressModel->where(['busid' => $params['busid']])->update(['status' => 0]);

        if ($status === FALSE) {
            $this->error('修改默认地址异常');
        }

        $result = $this->AddressModel->isUpdate(true)->save(['id' => $params['addrid'], 'status' => 1]);

        if ($result === FALSE) {
            $this->error('设置默认收货地址失败');
        } else {
            $this->success('设置默认收货地址成功');
        }
    }

    public function default()
    {
        $busid = $this->request->param('busid', 0, 'trim');

        $address = $this->AddressModel->where(['busid' => $busid, 'status' => 1])->find();

        if (!$address) {
            $this->error('默认收货地址不存在');
        }

        $this->success('查询成功', null, $address);
    }
}
